package com.os.opencv.java.chapter11;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class GoodFeaturesToTrack {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat src = Imgcodecs.imread("pics/face.jpg", Imgproc.COLOR_BGR2GRAY);
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        HighGui.imshow("gray", gray);
        HighGui.waitKey(0);

        //参数准备
        MatOfPoint corners = new MatOfPoint();  //检测结果集
        int maxCorners = 20;  //最多角点数
        double qualityLevel = 0.01;  //质量等级
        double minDistance = 10;  //角点间的最小距离

        //用harris算法进行检测
        boolean useHarrisDetector = true;
        Imgproc.goodFeaturesToTrack(gray, corners,maxCorners, qualityLevel, minDistance, new Mat(), 3, useHarrisDetector, 0.04);

        //将harris角点检测结果用圆圈画出
        Mat dst = src.clone();
        Point[] p = corners.toArray();  //转换为数组
        for(int i=0; i<p.length; i++){
            Imgproc.circle(dst, p[i], 3, new Scalar(0,0,255), 3);
        }
        //在屏幕上显示harris角点检测结果
        HighGui.imshow("harris", dst);
        HighGui.waitKey(0);

        //用shi-tomasi算法进行检测
        useHarrisDetector = false;
        Imgproc.goodFeaturesToTrack(gray, corners, maxCorners, qualityLevel, minDistance, new Mat(), 3, useHarrisDetector, 0.04);

        dst = src.clone();
        p = corners.toArray();
        for(int i=0; i<p.length; i++){
            Imgproc.circle(dst, p[i], 3, new Scalar(0,0,255), 3);
        }
        //在屏幕上显示shi-tomasi角点检测结果
        HighGui.imshow("shi-tomasi", dst);
        HighGui.waitKey(0);
        System.exit(0);
    }
}
